﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class QuanTri_QuanTriTin_NewsHtml : System.Web.UI.UserControl
{
    NewsHtmlDAO dao = new NewsHtmlDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            visiblePanel(true, false);
            bindGrid();
        }
    }
    private void bindGrid()
    {

        gridModule.DataSource = dao.getAll();
        gridModule.DataBind();
    }
    protected void cmdCapNhatDetail_Click(object sender, EventArgs e)
    {
        if(string.IsNullOrEmpty(lblId.Text))//insert
        {
            dao.id = string.Empty;
            dao.tenTrang = txtTenTrang.Text;
            dao.ghiChu = txtGhiChu.Text;
            dao.status = cboStatus.SelectedValue;
            dao.noiDung = fck.Value;
            if(dao.insert()>0)
            {
                visiblePanel(true,false);
                bindGrid();
            }

        }
        else//update
        {
            dao.id = lblId.Text;
            dao.tenTrang = txtTenTrang.Text;
            dao.ghiChu = txtGhiChu.Text;
            dao.status = cboStatus.SelectedValue;
            dao.noiDung = fck.Value;
            if(dao.update()>0)
            {
                visiblePanel(true, false);
                bindGrid();
            }

        }
    }
    protected void cmdHuyBoDetail_Click(object sender, EventArgs e)
    {
        visiblePanel(true,false);
    }
    private void visiblePanel(bool grid, bool edit)
    {
        pnGrid.Visible = grid;
        pnEdit.Visible = edit;
    }
    protected void gridModule_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName.Equals("Edit"))
        {
            lblId.Text = e.Item.Cells[0].Text;
            dao.id = lblId.Text;
            if (dao.getById())
            {
                txtTenTrang.Text = dao.tenTrang;
                txtGhiChu.Text = dao.ghiChu;
                fck.Value = dao.noiDung;
                try
                {
                    cboStatus.SelectedValue = dao.status;
                }
                catch { }
                visiblePanel(false, true);
            }
            else
            {
                MyWebUtils.MyWebUtil.alert(Page,"Có lỗi khi lấy thông tin để sửa");
            }
        }
        if (e.CommandName.Equals("Delete"))
        {
            dao.id = e.Item.Cells[0].Text;
            if (dao.delete() > 0)
            {
                bindGrid();
            }
        }
    }
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        lblId.Text = string.Empty;
        visiblePanel(false,true);
    }
}
